package swing;

import swing.frame.DesktopWordFrame;
import swing.frame.LoginFrame;
import swing.frame.MainFrame;
import swing.frame.ProfileFrame;

import javax.swing.*;

/**
 * 全局窗口管理器
 * 每个窗口保持唯一实例，避免重复创建
 */
public class FrameManager {

    public static LoginFrame loginFrame;
    public static MainFrame mainFrame;
    public static ProfileFrame profileFrame;
    public static DesktopWordFrame desktopWordFrame;

    /**
     * 显示登录界面
     */
    public static void showLoginFrame() {
        if (loginFrame == null || !loginFrame.isEnabled()) {
            loginFrame = new LoginFrame();
        }
        loginFrame.showFrame();
    }

    /**
     * 显示程序主界面
     */
    public static void showMainFrame() {
        if (mainFrame == null || !mainFrame.isEnabled()) {
            mainFrame = new MainFrame();
        }
        mainFrame.showFrame();
    }

    /**
     * 隐藏程序主界面
     */
    public static void hideMainFrame() {
        if (mainFrame != null && mainFrame.isEnabled()) {
            mainFrame.hideFrame();
        }
    }

    /**
     * 关闭个人主页
     */
    public static void closeProfileFrame() {
        if (profileFrame != null && profileFrame.isEnabled()) {
            profileFrame.dispose();
            profileFrame = null;
        }
    }

    /**
     * 显示/不显示个人主页，固定在主界面左侧
     */
    public static void reverseProfileFrameState(JFrame parentFrame) {
        if (profileFrame == null || !profileFrame.isEnabled()) {
            profileFrame = new ProfileFrame();
        }
        //JFrame会比它的容器JPanel大一圈
        int x = parentFrame.getX() + parentFrame.getWidth() - 45;
        int y = parentFrame.getY();
        profileFrame.setLocation(x, y);
        profileFrame.setVisible(!profileFrame.isVisible());
    }

    public static void flushProfileAchievement() {
        if (profileFrame != null && profileFrame.isEnabled()) {
            profileFrame.flushAchievement();
        }
    }

    public static void offDesktopWordSwitch() {
        if (profileFrame != null && profileFrame.isEnabled()) {
            profileFrame.offDesktopWordSwitch();
        }
    }

    /**
     * 显示桌面单词
     */
    public static void showDesktopWordFrame(int refreshTime) {
        if (desktopWordFrame == null || !desktopWordFrame.isEnabled()) {
            desktopWordFrame = new DesktopWordFrame();
        }
        desktopWordFrame.showFrame(refreshTime);
    }

    /**
     * 隐藏桌面单词
     */
    public static void hideDesktopWordFrame() {
        if (desktopWordFrame != null && desktopWordFrame.isEnabled()) {
            desktopWordFrame.hideFrame();
        }
    }

    /**
     * 修改桌面单词刷新时间
     */
    public static void changeWordRefreshTime(int refreshTime) {
        if (desktopWordFrame != null && desktopWordFrame.isEnabled()) {
            desktopWordFrame.changeRefreshTime(refreshTime);
        }
    }
}
